package com.tlxch.server.teacher.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tlxch.dto.*;
import com.tlxch.mapper.*;
import com.tlxch.pojo.SStudent;
import com.tlxch.pojo.TAssess;
import com.tlxch.result.PageResult;
import com.tlxch.server.teacher.TAssessService;
import com.tlxch.vo.AssessMsgvo;
import com.tlxch.vo.StudentGradevo;
import com.tlxch.vo.TAssessvo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class TAssessServiceImpl implements TAssessService {
    @Autowired
    TAssessMapper tAssessMapper;
    @Autowired
    SStudentMapper sStudentMapper;
    @Autowired
    SGradesMapper sGradesMapper;
    @Autowired
    SHabitMapper sHabitMapper;
    @Autowired
    SAttitudeMapper sAttitudeMapper;
    @Autowired
    SEffectMapper sEffectMapper;

    public PageResult pageQuery(TAssessdto tAssessdto) {
        PageHelper.startPage(tAssessdto.getPage(),tAssessdto.getPageSize());
        Page<LearnModeldto> learnModeldto = sGradesMapper.getStuOrBySsid(tAssessdto);
        for (LearnModeldto learnModeldto1 : learnModeldto) {
            List<Shabitdto> shabitdto = sHabitMapper.getByStuId(learnModeldto1.getSSid());
            learnModeldto1.setShabitdtos(shabitdto);
            List<Sattitudedto> sattitudedtoList = sAttitudeMapper.getByStuname(learnModeldto1.getSName());
            learnModeldto1.setSattitudedtos(sattitudedtoList);
            List<Seffectdto> seffectdtoList = sEffectMapper.getByStuId(learnModeldto1.getSSid());
            learnModeldto1.setSeffectdtos(seffectdtoList);
        }
        return new PageResult(learnModeldto.getTotal(), learnModeldto.getResult());
    }

    public void saveAssess(List<TAssess> tAssessList) {
        List<TAssess> tAssesss = new ArrayList<>();
        for (TAssess tAssess : tAssessList) {
            tAssess.setTsCreatetime(LocalDateTime.now());
            if (tAssess.getTsType() == 3) {
                continue;
            }
            if (tAssess.getTsType() == 2) {
                if (tAssess.getTsContent() == null || tAssess.getTsContent().equals("")) {
                    if (tAssess.getTsLevel() == 1){
                        tAssess.setTsContent("心理状态良好，继续保持！");
                    }else if (tAssess.getTsLevel() == 2){
                        tAssess.setTsContent("心理状态一般，需注意！");
                    }else if (tAssess.getTsLevel() == 3){
                        tAssess.setTsContent("心理状态较差，需关注！");
                    }else {
                        tAssess.setTsContent("心理状态达到预警状态，请及时关注！");
                    }
                }
            }
            tAssesss.add(tAssess);
        }
        tAssessMapper.saveAssess(tAssesss);
    }

    public PageResult assessPage(TAssessdto tAssessdto) {
        PageHelper.startPage(tAssessdto.getPage(),tAssessdto.getPageSize());
        Page<TAssessvo> learnModeldto = tAssessMapper.assessPage(tAssessdto);
        return new PageResult(learnModeldto.getTotal(), learnModeldto.getResult());
    }

}
